package com.zhsm.fuxi.mvp.ui.activitys

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.hankkin.reading.base.BaseMvpActivity
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.zhsm.fuxi.R
import com.zhsm.fuxi.bean.InvoiceDetailResp
import com.zhsm.fuxi.bean.InvoiceHistoryResp
import com.zhsm.fuxi.mvp.contract.InvoiceHistoryContract
import com.zhsm.fuxi.mvp.presenter.InvoiceHistoryPresenter
import com.zhsm.fuxi.mvp.ui.adapter.InvoiceHistoryRecyclerAdapter
import com.zhsm.paipaiout.baselibrary.widget.view.PageLayout
import kotlinx.android.synthetic.main.activity_invoice_history.*
import kotlinx.android.synthetic.main.layout_head_bar.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by GuZhC on 2018/10/19.
 * describe:
 */
class InvoiceHistoryActivity : BaseMvpActivity<InvoiceHistoryPresenter>(), InvoiceHistoryContract.IView,
        PageLayout.OnRetryClickListener, OnRefreshLoadMoreListener {

    val mAdapter: InvoiceHistoryRecyclerAdapter = InvoiceHistoryRecyclerAdapter(ArrayList<InvoiceDetailResp>())
    val mPageLayout by lazy {
        PageLayout.Builder(this)
                .initPage(findViewById(R.id.invoiceHistoryRecycler))
                .setDefaultEmptyText("暂无开票历史")
                .setEmptyDrawable(R.mipmap.empty_order)
                .setDefaultErrorText("网络错误，请点击刷新")
                .setErrorDrawable(R.mipmap.error_net)
                .setOnRetryListener(this)
                .create()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_invoice_history
    }

    override fun registerPresenter() = InvoiceHistoryPresenter::class.java


    override fun initView() {
        setMiuiStatusBar()
        setToolBar()
        invoiceHistoryRecycler.layoutManager = LinearLayoutManager(this)
        invoiceHistoryRecycler.adapter = mAdapter
        invoiceHistoryRefresh.setOnRefreshLoadMoreListener(this)
        mAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, view, position ->
            val intent = Intent(this@InvoiceHistoryActivity, InvoiceDetaileActivity::class.java)
            intent.putExtra("invoiceId", mAdapter.data[position].id.toString())
            startActivity(intent)
        }
    }

    private fun setToolBar() {
        invoiceHistoryHeadBar.imgLift.onClick { finish() }
        invoiceHistoryHeadBar.tvTitle.text = "开票历史"
    }

    var pager = 1
    override fun initData() {
        super.initData()
        invoiceHistoryRefresh.autoRefresh()
    }

    override fun onRetry() {
        invoiceHistoryRefresh.autoRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout?) {
        if (haveMore)
            getPresenter().getInvoiceHistory(pager)
        else invoiceHistoryRefresh.finishLoadMoreWithNoMoreData()
    }

    override fun onRefresh(refreshLayout: RefreshLayout?) {
        pager = 1
        haveMore = true
        invoiceHistoryRefresh.isEnableLoadMore = true
        invoiceHistoryRefresh.setNoMoreData(false)
        getPresenter().getInvoiceHistory(pager)
    }

    var haveMore = true
    override fun getInvoiceHistorySucess(it: InvoiceHistoryResp?) {
        if (it == null) getInvoiceHistoryFail()
        it?.run {
            if (list != null) {
                if (pager == 1) {
                    mAdapter.replaceData(list)
                    invoiceHistoryRefresh.finishRefresh(0, true)
                } else {
                    mAdapter!!.addData(list)
                    invoiceHistoryRefresh.finishLoadMore(0, true, false)
                }
                if (list?.size == pageSize) pager++
                else haveMore = false
            } else{
                invoiceHistoryRefresh.finishRefresh(0, true)
                invoiceHistoryRefresh.finishLoadMore(0, true, false)
                haveMore = false
            }
            if (mAdapter.data.isEmpty()) {
                mPageLayout.showEmpty()
                invoiceHistoryRefresh.isEnableLoadMore = false
            } else mPageLayout.hide()
        }
    }

    override fun getInvoiceHistoryFail() {
        invoiceHistoryRefresh.finishRefresh(0, false)
        invoiceHistoryRefresh.finishLoadMore(0, false, false)
        mPageLayout.showError()
        invoiceHistoryRefresh.isEnableLoadMore = false
    }
}
